<template>
	<view class="container">
		<bt-cropper ref="cropper" :autoZoom="false" :imageSrc="imageSrc"></bt-cropper>
		<view class="mainpadding_top">
			<view class="submitbtn margin_top" style="background-color: #c1282d;" @click="crop">保存到扫描相册</view>
		</view>
	</view>
</template>
<style lang="scss" scoped>
	.container {
		/** 外层一定要指定大小 */
		height: 80vh;
	}
</style>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				imageSrc: "",
			}
		},
		onLoad(options) {
			this.imageSrc = options.url
		},
		methods: {
			crop() {
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				let that = this
				// 通过组件定义的ref调用cropper方法，返回一个promise对象
				this.$refs.cropper.crop().then((res) => {
					console.log(res)
					// #ifdef APP-PLUS
					that.uploadImageService(res)
					// #endif
					// #ifdef H5
					that.uploadImageService(that.imageSrc)
					// #endif
				}).catch(err=>{
					uni.hideLoading()
				})
			},
			uploadImageService(url) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					console.log(image_item, "导出图片地址");
					this.daochu(res.url)
				}).catch(err => {
					uni.hideLoading()
				});
			},
			daochu(url){
				httpRequest.request('/api/user/addUserImages', 'GET', {
					image:url
				}).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
			}
		}
	}
</script>